package Controller;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;
import javax.swing.*;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html");
        response.setCharacterEncoding("utf-8");
        String zhanghao = request.getParameter("zhanghao");
        String pwd = request.getParameter("pwd");
        String iden = request.getParameter("user_type");
        int identity = Integer.parseInt(iden);
        HttpSession session = request.getSession();

        try {
            Context context = new InitialContext();
            Context contextNeeded = (Context) context.lookup("java:comp/env");
            DataSource ds = (DataSource) contextNeeded.lookup("wq");// 获得连接池。
            Connection conn = null;
            ResultSet rs;
            String sql;
            PreparedStatement pstmt;
            conn = ds.getConnection();//获得数据源
            sql = "select * from user where id=? and password=? and identity=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, zhanghao);
            pstmt.setString(2, pwd);
            pstmt.setInt(3,identity);
            rs = pstmt.executeQuery();
            if (rs.next()) {
                if(rs.getInt("identity")==0){//0管理员
                    session.setAttribute("name",rs.getString("name"));
                    response.sendRedirect("admin/admin_index.jsp");
                }
                else if(rs.getInt("identity")==1){//1裁判
                    session.setAttribute("name",rs.getString("name"));
                    session.setAttribute("id",rs.getString("id"));
                    response.sendRedirect("caipan/caipan_index.jsp");
                }
                else if(rs.getInt("identity")==2){//2运动员
                    session.setAttribute("name",rs.getString("name"));
                    session.setAttribute("id",rs.getString("id"));
                    response.sendRedirect("sporter/sporter_index.jsp");
                }
                else if(rs.getInt("identity")==3){//3志愿者
                    session.setAttribute("name",rs.getString("name"));
                    session.setAttribute("id",rs.getString("id"));
                    response.sendRedirect("zhiyuanzhe/zhiyuanzhe_index.jsp");
                }
            } else {
                // System.out.println("不存在");
                response.getWriter().write("<script language='javascript'>alert('登录失败！');" +
                        "window.location.href='/jsp/login.jsp';</script>");
//                response.sendRedirect("jsp/login.jsp");
            }
            conn.close();
        } catch (SQLException e) {
            System.out.println("加载不成功");
        } catch (NamingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
